<template>
  <form-item-wrapper
    :designer="designer"
    :field="field"
    :rules="rules"
    :design-state="designState"
    :parent-id="parentId"
    :parent-list="parentList"
    :index-of-parent-list="indexOfParentList"
  >
    <div v-if="previewStatus" class="inner_value">
      {{ getLabelByValue(formData[fieldId]) }}
    </div>
    <el-radio-group
      v-else
      ref="fieldEditor"
      v-model="formData[fieldId]"
      :disabled="field.options.disabled"
      :size="field.options.size"
      @change="handleChangeEvent"
    >
      <template v-if="!!field.options.buttonStyle">
        <el-radio-button
          v-for="(item, index) in field.options.optionItemsAsync !== ''
            ? customOptionItems
            : field.options.optionItems"
          :key="index"
          :label="item.value"
          :disabled="item.disabled"
          :border="field.options.border"
          :style="{ display: field.options.displayStyle }"
        >
          {{ item.label }}
        </el-radio-button>
      </template>
      <template v-else>
        <el-radio
          v-for="(item, index) in field.options.optionItemsAsync !== ''
            ? customOptionItems
            : field.options.optionItems"
          :key="index"
          :label="item.value"
          :disabled="item.disabled"
          :border="field.options.border"
          :style="{ display: field.options.displayStyle }"
        >
          {{ item.label }}
        </el-radio>
      </template>
    </el-radio-group>
  </form-item-wrapper>
</template>

<script>
/* eslint-disable vue/require-default-prop */
/* eslint-disable eol-last */
import FormItemWrapper from "./form-item-wrapper";
import emitter from "Yan/utils/emitter";
import i18n from "Yan/utils/i18n";
import fieldMixin from "./fieldMixin";

export default {
  name: "radio_unit",
  componentName: "FieldWidget",
  components: {
    FormItemWrapper,
  },
  mixins: [emitter, fieldMixin, i18n],
  props: {
    designer: Object,
    fieldId: String,
    parentId: String,
    parentList: Array,
    indexOfParentList: Number,
    formData: Object,
    designState: {
      type: Boolean,
      default: false,
    },
  },
  // inject: ["refList", "formConfig", "globalOptionData", "globalModel"],
  data() {
    return {
      oldFieldValue: null,
      // fieldModel: null,
      customOptionItems: [],
      rules: [],
    };
  },
  beforeCreate() {
    /* 这里不能访问方法和属性！！ */
  },

  created() {
    /* 注意：子组件mounted在父组件created之后、父组件mounted之前触发，故子组件mounted需要用到的prop
         需要在父组件created中初始化！！ */
    // this.initOptionItems();
    // this.initFieldModel();
    // this.registerToRefList();
    this.initEventHandler();
    this.buildFieldRules();

    this.handleOnCreated();
    this.handleOptionItemsAsync();
  },

  mounted() {
    this.handleOnMounted();
  },

  // beforeDestroy() {
  //   this.unregisterFromRefList();
  // },

  methods: {
    getLabelByValue(value) {
      let options = this.field.options.optionItems;
      if (this.field.options.optionItemsAsync !== "") {
        // options = await this.handleOptionItemsAsync();
        options = this.customOptionItems;
      }
      // console.log("getLabelByValue", value, options);
      const res = options.find((i) => i.value === value);
      return res ? res.label : "";
    },
  },
};
</script>

<style lang="scss" scoped>
@import "../../../../styles/global.scss"; //* form-item-wrapper已引入，还需要重复引入吗？ *//
</style>
